package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.api.service.AfterSaleService;
import cn.e4j.modular.api.service.BusinessService;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("api/wx/aftersale")
public class AfterSaleController extends ControllerSupport {
    @Autowired
    BusinessService businessService;

    @Autowired
    AfterSaleService saleService;

    @Autowired
    Gwat200Service gwat200Service;

    @Autowired
    SystemServiceImpl systemService;

    //获取商品详情
    @PostMapping("getEchartData")
    public ResponseData getEchartData(@DtoParam DtoWapper wapper){
        Dto indto = wapper.getDto();
        Dto dto = new Dto();
        try {
            dto.put("lineData",saleService.getTime(indto));
            dto.put("pieData1",saleService.getMMRG(indto));
            dto.put("pieData2",saleService.getMMRD(indto));
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(dto);
    }

    //满意度分析
    @PostMapping("getMMRK")
    public ResponseData getMMRK(@DtoParam DtoWapper wapper){
        List<Dto> dtoList = null;
        try {
            dtoList = saleService.getMMRK(wapper.getDto());
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(dtoList);
    }

    @PostMapping("getNum")
    public ResponseData getNum(@DtoParam DtoWapper wapper){
        List<Dto> dtoList = null;
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWAA003", currentUser.getACCOUNT());
            inDto.put("MMRE004S", gwat200Service.getMmreData(inDto));
            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(currentUser,(List<Dto>)sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));

            dtoList = saleService.getCOUNT(inDto);
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(dtoList);
    }


    @PostMapping("getTodayTask")
    public ResponseData getTodayTask(@DtoParam DtoWapper wapper){
        List<Dto> dtoList = null;
        Dto dto = wapper.getDto();
        try {
            dto.put("now", LocalDate.now());
            dto.put("MMRF007", wapper.getSysUser().getID());//员工编号
            dtoList = saleService.getTodayTask(dto);
        }catch (Exception e){
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(dtoList);
    }

}
